﻿//zTree setting设置
var zTreeObj;
var setting = {
    async: {
        enable: true,
        contentType: "application/json",
        dataType: "json",
        url: "/Menu/GetMenuTree",
        type: "get"
    },
    data: {
        key: {
            name: "MenuName"
        },
        simpleData: {
            enable: true,
            idKey: "Id",
            pIdKey: "ParentId"

        }
    },
    callback: {
        onClick: zTreeOnClick
        //        ,onDblClick: zTreeOnDblClick
        //        ,onAsyncSuccess: zTreeOnAsyncSuccess
    }
};
$(function () {
    zTreeObj = $.fn.zTree.init($("#treeMenu"), setting);
});

var table1;
var url;
function zTreeOnClick(event, treeId, treeNode) {
    $("#ParentId").val(treeNode.Id);
    $("#ParentName").val(treeNode.MenuName);


    url = "/Menu/GetMenu?ParentId=" + $("#ParentId").val()
    table1 = $().hiTable({
        tableId: "Searchresult",
        pageBarId: "PageBar1",
        ajaxURL: url,
        DefaultSortTitle: "Sort",
        DefaultSortOrder: "0",
        columnsConfig: [
                { Title: 'MenuName' },
                { Title: 'Description' },
                {
                    FnRender: function (item) {
                        return item["IsMenu"] == 1 ? bYes : bNo;
                    }
                },
                {
                    FnRender: function (item) {
                        return item["IsDisplay"] == 1 ? bYes : bNo;
                    }
                },
                { Title: 'LinkURL' },
                {
                    FnRender: function (item) {
                        var MenuId = "'" + item["Id"] + "'";
                        return '<img  alt="修改"  style=\"cursor:pointer\" onclick="Edit(' + MenuId + ')" src="../../Content/images/edit.png"/>&nbsp;&nbsp;&nbsp;' + '<img  alt="删除" style=\"cursor:pointer\" onclick="Delete(' + MenuId + ')" src="../../Content/images/delete.png" />'
                        + '&nbsp;&nbsp;&nbsp;<img  alt="向上"  style=\"cursor:pointer\" onclick="MoveUp(' + MenuId + ')" src="../../Content/images/up.png"/>&nbsp;&nbsp;&nbsp;' + '<img  alt="向下" style=\"cursor:pointer\" onclick="MoveDown(' + MenuId + ')" src="../../Content/images/down.png" />';
                    }
                }
                ]
    });

    //改变每页显示条数
    $("#perPage").change(function () {
        table1.loadData(parseInt($(this).val()), 0);
    });

    //隐藏前面的checkbox
    table1.loadComplete = function (pageSize, selectPageIndex) {
        $("#Searchresult  td").has(":checkbox").hide();
    }

    //点击时，加载数据
    table1.loadData(parseInt($("#perPage").val()), 0);
};

//添加按钮方法
function AddMenu() {
    var treeObj = $.fn.zTree.getZTreeObj("treeMenu");
    var nodes = treeObj.getSelectedNodes();
    if (nodes.length < 1) {
        alert(ChooseAMenu);
        return;
    }
    var url = "/Menu/Add?ParentId=" + $("#ParentId").val() + "&ParentName=" + $("#ParentName").val();
    window.location.href = url;
}

//移动按钮方法
function MoveUp(Id) {
    $.post('/Menu/MoveUpMenu', {
        Id: Id,
        ParentId: $("#ParentId").val()
    }, function (data) {
        //        if (data == "True") {
        //重新加载页面               
        table1.loadData(parseInt($("#perPage").val()), 0);
        zTreeObj = $.fn.zTree.init($("#treeMenu"), setting);
        //        }
    })
}

function MoveDown(Id) {
    $.post('/Menu/MoveDownMenu', {
        Id: Id,
        ParentId: $("#ParentId").val()
    }, function (data) {
        //        if (data == "True") {
        //重新加载页面               
        table1.loadData(parseInt($("#perPage").val()), 0);
        zTreeObj = $.fn.zTree.init($("#treeMenu"), setting);
        //        }
    })
}

//删除按钮方法
function Delete(Id) {
    if (confirm(SureDelete)) {
        $.post('/Menu/DeleteMenu', {
            Id: Id
        }, function (data) {
            if (data == "1") {
                //重新加载页面               
                table1.loadData(parseInt($("#perPage").val()), 0);
                zTreeObj = $.fn.zTree.init($("#treeMenu"), setting);
            }
            else if (data == "0") {
                alert(DeleteFail);
            }
            else if (data == "-1") {
                alert(DeleteFail_DeleteChildFirst);
            }
        })
    }
}

//编辑按钮方法
function Edit(Id) {
    var url = "/Menu/Edit?Id=" + Id;
    window.location.href = url;
}